﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TJDevHouse.Starcraft.Game;

namespace TJDevHouse.Starcraft.AIModuleConnector.ImportExport
{
	/// <summary>
	/// Handles importing the unit type data from the AIModule
	/// </summary>
	public class RawUnitTypeDataImporter
	{
		public Dictionary<TJDevHouse.Starcraft.Game.UnitType, TJDevHouse.Starcraft.Game.UnitTypeSpec> ParseUnitTypeData(string unitTypeData)
		{
			Dictionary<TJDevHouse.Starcraft.Game.UnitType, TJDevHouse.Starcraft.Game.UnitTypeSpec> types = new Dictionary<TJDevHouse.Starcraft.Game.UnitType, TJDevHouse.Starcraft.Game.UnitTypeSpec>();

			System.String[] unitTypes = unitTypeData.Split(":".ToCharArray());
			bool first = true;

			foreach (String unitType in unitTypes)
			{
				if (first)
				{
					first = false;
					continue;
				}

				System.String[] attributes = unitType.Split(";".ToCharArray());
				TJDevHouse.Starcraft.Game.UnitTypeSpec type = new TJDevHouse.Starcraft.Game.UnitTypeSpec();

				type.ID = System.Int32.Parse(attributes[0]);
				type.UnitType = (TJDevHouse.Starcraft.Game.UnitType)type.ID;
				type.Race = TJDevHouse.Starcraft.Game.Races.GetRaceByBWAPIRaceID(System.Int32.Parse(attributes[1]));

				//Console.WriteLine(type.TJDevHouse.Starcraft.Game.UnitType + " has race id of " + System.Int32.Parse(attributes[1]));

				type.Name = attributes[2];
				type.MineralsCost = System.Int32.Parse(attributes[3]);
				type.GasCost = System.Int32.Parse(attributes[4]);
				type.MaxHitPoints = System.Int32.Parse(attributes[5]);
				type.MaxShields = System.Int32.Parse(attributes[6]);
				type.MaxEnergy = System.Int32.Parse(attributes[7]);
				type.BuildTime = System.Int32.Parse(attributes[8]);
				type.CanAttack = System.Int32.Parse(attributes[9]) == 1;
				type.CanMove = System.Int32.Parse(attributes[10]) == 1;
				type.TileWidth = System.Int32.Parse(attributes[11]);
				type.TileHeight = System.Int32.Parse(attributes[12]);
				type.SupplyRequired = System.Int32.Parse(attributes[13]);
				type.SupplyProvided = System.Int32.Parse(attributes[14]);
				type.SightRange = System.Int32.Parse(attributes[15]);
				type.GroundMaxRange = System.Int32.Parse(attributes[16]);
				type.GroundMinRange = System.Int32.Parse(attributes[17]);
				type.GroundDamage = System.Int32.Parse(attributes[18]);
				type.AirRange = System.Int32.Parse(attributes[19]);
				type.AirDamage = System.Int32.Parse(attributes[20]);
				type.IsABuilding = System.Int32.Parse(attributes[21]) == 1;
				type.IsAFlyer = System.Int32.Parse(attributes[22]) == 1;
				type.IsASpellCaster = System.Int32.Parse(attributes[23]) == 1;
				type.IsAWorker = System.Int32.Parse(attributes[24]) == 1;
				type.WhatBuilds = (TJDevHouse.Starcraft.Game.UnitType)System.Int32.Parse(attributes[25]);

				//System.Console.Out.WriteLine(type.Id + " " + type.whatBuilds);

				types.Add(type.UnitType, type);
			}

			return types;
		}
	}
}
